// created by noellynch
// Jun 3, 2009

package com.sceneworker.monitorpopup.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import com.jme.image.Texture;
import com.jme.image.Texture2D;
import com.jme.image.Texture.DepthTextureCompareFunc;
import com.jme.image.Texture.DepthTextureCompareMode;
import com.jme.image.Texture.DepthTextureMode;
import com.jme.image.Texture.RenderToTextureType;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.TextureRenderer;
import com.jme.scene.Geometry;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SimpleAlert;
import com.myjavautils.swing.SwingComponentFactory;
import com.sceneworker.app.globals.SceneWorkerGlobals;
import com.sceneworker.app.globals.cacheobjects.RenderToTextureCacheObject;

public class NewTextureRendererPanel extends JPanel {
	
	private	JCheckBox			mcl_depth;
	
	public	NewTextureRendererPanel(final TextureState cl_ts, final Object cl_owner, final JDialog cl_dlg) {
		setLayout(new MigLayout("inset 5"));
		
		final	JTextField		lcl_width = MigLayoutUtils.numberText(this, "Width", 100, true);
		final	JTextField		lcl_height = MigLayoutUtils.numberText(this, "Height", 100, true);
		
		JButton			lcl_lockToScreenDim = SwingComponentFactory.makeAButton("Screen Dimensions");
		lcl_lockToScreenDim.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DisplaySystem	lcl_display = DisplaySystem.getDisplaySystem();
				lcl_width.setText("" + lcl_display.getWidth());
				lcl_height.setText("" + lcl_display.getHeight());
			}
		});
		
		JButton			lcl_create = SwingComponentFactory.makeAButton("Create");
		lcl_create.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createTextureRenderer(cl_ts, Integer.parseInt(lcl_width.getText()), Integer.parseInt(lcl_height.getText()), cl_owner);
				cl_dlg.setVisible(false);
			}
		});
		
		JButton			lcl_cancel = SwingComponentFactory.makeAButton("Cancel");
		lcl_cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl_dlg.setVisible(false);
			}
		});
		
		mcl_depth = SwingComponentFactory.makeACheckBox("Depth Texture", false);
		/*add(mcl_depth, "wrap");*/
		
		MigLayoutUtils.makeRightAlignedButtonBar(this, lcl_lockToScreenDim, lcl_create, lcl_cancel);
	}
	
	private	void	createTextureRenderer(final TextureState cl_ts, final int i_w, final int i_h, final Object cl_s) {
		GameTaskQueueManager.getManager().update(new Callable<Void>() {
			public Void call() throws Exception {
				TextureRenderer		lcl_tr = DisplaySystem.getDisplaySystem().createTextureRenderer(i_w, i_h, TextureRenderer.Target.Texture2D);
				
				if(!lcl_tr.isSupported()) {
					SimpleAlert.doAlert("Render To Texture is not supported.", null);
					return null;
				}
				
				Texture2D		lcl_tex = new Texture2D();
				lcl_tex.setWrap(Texture.WrapMode.Clamp);
				if(mcl_depth.isSelected()) {
					lcl_tex.setRenderToTextureType(RenderToTextureType.Depth);
					lcl_tex.setDepthCompareMode(DepthTextureCompareMode.RtoTexture);
					lcl_tex.setDepthMode(DepthTextureMode.Intensity);
					lcl_tex.setDepthCompareFunc(DepthTextureCompareFunc.GreaterThanEqual);
				} else {
					lcl_tr.setBackgroundColor(new ColorRGBA(.667f, .667f, .851f, 1f));
				}
				//
				
				lcl_tr.setupTexture(lcl_tex);
				lcl_tr.getCamera().setLocation(new Vector3f(0, 0, 75f));
		        
				cl_ts.setTexture(lcl_tex, cl_ts.getNumberOfSetTextures());
				if(cl_s instanceof Geometry) {
					((Geometry)cl_s).copyTextureCoordinates(0, cl_ts.getNumberOfSetTextures() - 1, 1);
				}
				
				cl_ts.setNeedsRefresh(true);
				if(cl_s != null) {
					if(cl_s instanceof Spatial) {
						((Spatial) cl_s).updateRenderState();
						((Spatial) cl_s).updateGeometricState(0, true);
					}
				}
				
				Node		lcl_scene = new Node("RTT Scene");
				
				SceneWorkerGlobals.inst().addToCache(new RenderToTextureCacheObject(lcl_tr, lcl_tex, lcl_scene));
				
				return null;
			}
		});
	}
}
